﻿using System.Net.Http.Json;
using System.Text;
using System.Text.Json;

namespace AiSk
{
    public static class HttpClientExtend
    {
        public record HttpClientExtendResponse<T>(HttpResponseMessage HttpResponseMsg) { public T? Result { get; set; } };

        private static async Task<HttpResponseMessage> UrlRequest
            (this HttpClient client,
            string url,
            string method,
            object content,
            Dictionary<string, string>? head = null,
            string contentType = "application/json")
        {
            var httpMethod = HttpMethod.Parse(method);
            var request = new HttpRequestMessage(httpMethod, url)
            {
                Content = new StringContent(content.GetType() == typeof(string) ? (content?.ToString() ?? string.Empty) : JsonSerializer.Serialize(content), Encoding.UTF8, contentType)
            };
            if (head != null)
            {
                foreach (var item in head)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }
            return await client.SendAsync(request);
        }


        public static async Task<HttpClientExtendResponse<T>> Request<T>
            (this HttpClient client,
            string url,
            string method,
            object content,
            Dictionary<string, string>? head = null,
            string contentType = "application/json")
        {
            var responseMsg = await UrlRequest(client, url, method, content, head, contentType);
            var res = new HttpClientExtendResponse<T>(responseMsg);
            if (responseMsg != null)
            {
                if (responseMsg.IsSuccessStatusCode)
                {
                    res.Result = await responseMsg.Content.ReadFromJsonAsync<T>();
                }
            }
            return res;
        }
        public static async Task<HttpClientExtendResponse<Stream>> RequestStream(this HttpClient client, string url, string method, object content, Dictionary<string, string>? head = null, string contentType = "application/json")
        {
            var responseMsg = await UrlRequest(client, url, method, content, head, contentType);
            var res = new HttpClientExtendResponse<Stream>(responseMsg);
            if (responseMsg != null)
            {
                if (responseMsg.IsSuccessStatusCode)
                {
                    res.Result = await responseMsg.Content.ReadAsStreamAsync();
                }
            }
            return res;
        }
        public static async Task<HttpClientExtendResponse<string>> Request(this HttpClient client, string url, string method, object content, Dictionary<string, string>? head = null, string contentType = "application/json")
        {
            var responseMsg = await UrlRequest(client, url, method, content, head, contentType);
            var res = new HttpClientExtendResponse<string>(responseMsg);
            if (responseMsg != null)
            {
                if (responseMsg.IsSuccessStatusCode)
                {
                    res.Result = await responseMsg.Content.ReadAsStringAsync();
                }
            }
            return res;
        }
    }
}
